package org.raymond.iworks.study.redis.redisson;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RFencedLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class FencedLockTest {
    public static void main(String[] args) throws Exception {
        test();
    }

    // 这种类型的锁会维护隔离令牌,以避免客户端由于长时间GC暂停或其他原因而延迟获取锁,从而无法检测到它不再拥有锁的情况.
    // 要解决这个问题,令牌由lockAndGetToken或getToken()方法返回.令牌应该检查是否大于或等于该锁保护的服务的前一个令牌,
    // 如果为false则拒绝操作
    public static void test() throws Exception {
        RedissonClient c = client();
        RFencedLock lock = c.getFencedLock("myFenced");

        log.info("shutdown");
        c.shutdown();
    }

    public static RedissonClient client(){
        Config cfg = new Config();
        cfg.useSingleServer().setAddress("redis://192.168.0.125:6379");
        RedissonClient client = Redisson.create(cfg);
        return client;
    }
}
